<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>addBrand</title>
</head>
<body>
<h3>添加品牌</h3>
<form method="post" action="" id="addForm">
    品牌名称：<input name="brandName" id="brandName"><br>
    企业名称：<input name="companyName" id="companyName"><br>
    排序：<input name="ordered" id="ordered"><br>
    描述信息：<textarea rows="5" cols="20" name="description" id="description"></textarea><br>
    状态：
    <input type="radio" name="status" value="0">禁用
    <input type="radio" name="status" value="1">启用
    <br>
    <input type="button" value="一键添加" onclick="add()">
</form>
<script src="../js/axios-0.18.0.js"></script><!--导入js-->
<script>
    //绑定单击事件
    /*function add() {
        //获取表单数据 --> 转为JSON
        let formData = {
            "brandName": "",
            "companyName": "",
            "ordered": "",
            "description": "",
            "status": ""
        };
        //给JSON设置值
        formData.brandName = document.getElementById("brandName").value;
        formData.companyName = document.getElementById("companyName").value;
        formData.ordered = document.getElementById("ordered").value;
        formData.description = document.getElementById("description").value;
        let status = document.getElementsByName("status");
        for (let i = 0; i < status.length; i++) {
            //status的值为单选按钮被选中时对应的值
            if (status[i].checked) {
                formData.status = status[i].value;
            }
        }
        console.log(formData);  //浏览器控制台打印formData
        //发送ajax请求
        //axios.post("http://localhost:8080/cookiesession_case/addServletJson", formData)
        axios({
            method: "post",
            url: "http://localhost:8080/cookiesession_case/addServletJson",
            data: formData
        }).then(function (response) {
            //处理响应数据
            console.log(response.data);
            let flag = response.data === 'success';
            if (flag) {
                alert("添加成功！");
                location.href = "http://localhost:8080/cookiesession_case/html/brand.html";
            }
        });
    }*/

    //快速将表单数据封装为JSON对象
    function add() {
        //获取表单
        let form = document.getElementById("addForm");
        //将表单数据封装成FormData对象
        let formData = new FormData(form);
        //定义请求参数的json对象
        let JsonParams = {};
        //遍历表单数据对象获取键值，封装到json对象中
        formData.forEach((v, k) => {
            JsonParams[k] = v;
        });
        //发送请求，传递参数
        axios.post("http://localhost:8080/cookiesession_case/addServletJson", JsonParams)
            //.then --》响应成功的回调
            .then(resp => {
                //处理响应
                if (resp.data === 'success') {
                    location.href = "http://localhost:8080/cookiesession_case/html/brand.html";
                }
            })
            //.catch --》响应失败的回调
            .catch(() => {
                //响应失败的逻辑
                alert("响应失败！")
            });
    }
</script>
</body>
</html>